#include "pythonhighlighter.h"

PythonHighlighter::PythonHighlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    HighlightingRule rule;

    keywordFormat.setForeground(Qt::darkBlue);
    keywordFormat.setFontWeight(QFont::Bold);
    QStringList keywordPatterns;
    keywordPatterns << "and" << "del" << "for" << "is" << "raise" <<
                       "assert" << "elif" << "from" << "lambda" << "return" <<
                       "break" << "else" << "global" << "not" << "try" <<
                       "class" << "except" << "if" << "or" << "while" <<
                       "continue" << "exec" << "import" << "pass" << "yield" <<
                       "def" << "finally" << "in" << "print";
    foreach (const QString &pattern, keywordPatterns) {
        rule.pattern = QRegExp("\\b" + pattern + "\\b");
        rule.format = keywordFormat;
        highlightingRules.append(rule);
    }

    /*
      not working at the moment
    operatorFormat.setForeground(Qt::lightGray);
    operatorFormat.setFontWeight(QFont::Bold);
    QStringList operatorPatterns;
    operatorPatterns << "+" << "-" << "\*" << "\*\*" << "/" << "//" << "%" << "<<" << ">>" << "&" << "|" << "^" << "\~" << "<" << ">" << "<=" << ">=" << "==" << "!=" << "<>";
    foreach (const QString& pattern, operatorPatterns) {
        rule.pattern = QRegExp(pattern);
        rule.format = operatorFormat;
        highlightingRules.append(rule);
    }*/

    classFormat.setFontWeight(QFont::Bold);
    classFormat.setForeground(Qt::darkMagenta);
    rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    rule.format = classFormat;
    highlightingRules.append(rule);

    singleLineCommentFormat.setForeground(Qt::red);
    rule.pattern = QRegExp("#[^\n]*");
    rule.format = singleLineCommentFormat;
    highlightingRules.append(rule);

    multiLineCommentFormat.setForeground(Qt::red);

    quotationFormat.setForeground(Qt::darkGreen);
    rule.pattern = QRegExp("\".*\"");
    rule.format = quotationFormat;
    highlightingRules.append(rule);
    rule.pattern = QRegExp("\'.*\'");
    highlightingRules.append(rule);

    functionFormat.setFontItalic(true);
    functionFormat.setForeground(Qt::blue);
    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
    rule.format = functionFormat;
    highlightingRules.append(rule);

    docstringStartExpression = QRegExp("\"\"\"");
    docstringEndExpression = QRegExp("\"\"\"");
}

void PythonHighlighter::highlightBlock(const QString &text)
{
    foreach (const HighlightingRule &rule, highlightingRules) {
        QRegExp expression(rule.pattern);
        int index = expression.indexIn(text);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = expression.indexIn(text, index + length);
        }
    }
    setCurrentBlockState(0);
    int startIndex = 0;
    if (previousBlockState() != 1)
        startIndex = docstringStartExpression.indexIn(text);

    while (startIndex >= 0) {
        int endIndex = docstringEndExpression.indexIn(text, startIndex);
        int commentLength;
        if (endIndex == -1) {
            setCurrentBlockState(1);
            commentLength = text.length() - startIndex;
        } else {
            commentLength = endIndex - startIndex
                            + docstringEndExpression.matchedLength();
        }
        setFormat(startIndex, commentLength, multiLineCommentFormat);
        startIndex = docstringStartExpression.indexIn(text, startIndex + commentLength);
    }
}
